<?php

namespace App\Http\Controllers\Blog;

use App\Models\Category;
use App\Models\Article;
use App\Models\Navbar;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class WidgetController extends Controller
{
    // 最近文章
    public function recentArticle()
    {
        return Cache::remember('blog.article.recent', 60 * 60 * 24, function () {
            return Article::publish()->orderBy('created_at', 'desc')->limit(10)->get(['id', 'title', 'url']);
        });
    }

    // 文章分类
    public function categoryList()
    {
        return Cache::remember('blog.article.category.list', 60 * 60 * 24, function () {
            return Category::where('pid', '<>', 0)->orderBy('sort', 'desc')->get(['id', 'name']);
        });
    }

    //导航
    public function navbar()
    {
        return Cache::remember('blog.navbar', 60 * 60 * 24, function () {
            return Navbar::where('is_show', 1)->orderBy('sort', 'desc')->get();
        });
    }
}
